package top.littlejiang.onlinexam.common.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.io.Serializable;
import java.util.List;


/**
 * 通用业务层
 * @author chen
 * @Description
 * @since 2020/12/26 16:31
 */
public abstract class BaseServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements BaseService<T> {

    /**
     * 根据条件查询一个
     * @param queryWrapper
     * @return
     */
    @Override
    public T selectOne(QueryWrapper<T> queryWrapper) {
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 根据批量逻辑删除
     *
     * @param ids
     */
    @Override
    public void logicDeleteById(List<Serializable> ids) {
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.set("is_del", 1);
        wrapper.in("id", ids);
        this.baseMapper.update(null, wrapper);
    }

    /**
     * 根据id 逻辑删除
     *
     * @param id
     */
    @Override
    public void logicDeleteById(Serializable id) {
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.set("is_del", 1);
        wrapper.eq("id", id);
        this.baseMapper.update(null, wrapper);
    }

    /**
     * 根据id 物理删除
     *
     * @param ids
     */
    @Override
    public void deleteById(List<Serializable> ids) {
        this.baseMapper.deleteBatchIds(ids);
    }
}
